<template>

    <tableCommon :tableData="tableData" :tableColumns="tableColumns" :total="total" :pageNumber="pageNumber"
                 :condtions="condtions" @pick="pick" @show="show"
                 @acquireItems="acquireItems"></tableCommon>
</template>

<script>
    import tableCommon from 'iview-base-element/src/views/tableCommon.vue';

    import $ from 'jquery'

    import axios from 'axios';

    export default {

        components: {tableCommon},

        data(){
            return {
                total: null,
                pageNumber: 1,
                tableData: [],
                tableColumns: [
                {
                    title: 'created_time_default',
                    key: 'created_time_default'
                },
                {
                    title: '名称',
                    key: 'name',
                    ellipsis: true,
                    render(row) {
                        return `<span title="${row.name}">${row.name}</span>`;
                    }
                },
                {
                    title: 'size',
                    key: 'size'
                },
                {
                    title: 'file_count',
                    key: 'file_count'
                },
                {
                    title: 'resource_create_date',
                    key: 'resource_create_date'
                }
                ,
                {
                    title: 'hot_count',
                    key: 'hot_count'
                }
                ,
                {
                    title: 'resource_url',
                    key: 'resource_url',
                    ellipsis: true
                }
                ,
                {
                    title: 'key_word',
                    key: 'key_word'
                },
                {
                    title: 'downloaded',
                    key: 'downloaded',

                    render(row)
                    {
                        return ['否','已下载'][row.downloaded];
                    }

                }


            ],
                condtions: {
                    name: {},
                    key_word: {},
                    size: {relationOperator:'regexp'},
                    downloaded : {relationOperator:'='}
                }
            };
        },
        methods: {
            pick(selection){

                let ids = [];

                let urls = [];

                _.forEach(selection, function(value) {
                    urls.push(value.resource_url);
                    ids.push(value.id);
                });

                let me = this;

                this.$Modal.confirm({
                    title: '确认对话框标题',
                    content: JSON.stringify(urls.join('<br/>')),
                    onOk: () => {
                        $.post("/magnetLinkResource/download", { "magnetLinkResourceIds": ids.join(",") },
                                function(data){
                                    if (data.success) {
                                        me.acquireItems(1, me.condtion);
                                        console.log('success',data);
                                        return;
                                    }
                                    console.log(data);
                                });

                },
                onCancel: () => {
                }
            });

            },
            show(data){
                this.$Modal.info({
                    content: `
                        name : ${data.name}<br/>
                        url  : ${data.resource_url}

                    `
                });
            },
            acquireItems(page, condtion){
                let me = this;
                axios.get('/magnetLinkResource', {
                    params: {
                        page: page,
                        condtion: condtion
                    }
                })
                        .then(function (response) {

                            var data = response.data;

                            me.total = data.totalRow;

                            me.tableData = data.list;

                            me.pageNumber = data.pageNumber;

                            console.log(response);
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
            }

        }

    }


</script>